{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "m1 = tf.constant([[3, 3]]) # 常量operation，生成一个Tensor\n",
    "m2 = tf.constant([[1], [2]]) # 常量operation，生成一个Tensor\n",
    "product = tf.matmul(m1, m2) # 加法operation，输入两个Tensor，生成一个Tensor\n",
    "\n",
    "with tf.Session() as session: # 利用Session执行上述代码中生成的DAG\n",
    "    res = session.run(product)\n",
    "    print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n",
      "[2 4]\n",
      "[3 6]\n",
      "[4 8]\n",
      "[ 5 10]\n",
      "[ 6 12]\n",
      "[ 7 14]\n",
      "[ 8 16]\n",
      "[ 9 18]\n",
      "[10 20]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "state = tf.Variable([0, 0], \"counter\") # 变量\n",
    "new_value = tf.add(state, [1, 2]) # 加法operation 生成一个Tensor\n",
    "upstate = tf.assign(state, new_value) # 赋值operation 生成一个Tensor\n",
    "\n",
    "init = tf.global_variables_initializer() # 变量初始化\n",
    "\n",
    "with tf.Session() as session:\n",
    "    session.run(init) # 变量初始化\n",
    "    for _ in range(10):\n",
    "        session.run(upstate) # 执行更新赋值也就是“+=”10次\n",
    "        print(session.run(state)) # 查看每次执行的效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[9]]), array([[12]])]\n"
     ]
    }
   ],
   "source": [
    "# fetch \n",
    "# fetch\n",
    "import tensorflow as tf\n",
    "m1 = tf.constant([[3, 3]]) # 常量operation，生成一个Tensor\n",
    "m2 = tf.constant([[1], [2]]) # 常量operation，生成一个Tensor\n",
    "m3 = tf.constant([3])\n",
    "product = tf.matmul(m1, m2) # 加法operation，输入两个Tensor，生成一个Tensor\n",
    "add = tf.add(product, m3)\n",
    "\n",
    "with tf.Session() as session: # 利用Session执行上述代码中生成的DAG\n",
    "    res = session.run([product, add]) # 同一个run中运行多个operation，operation之间可以有依赖关系\n",
    "    print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# feed\n",
    "import tensorflow as tf\n",
    "input1 = tf.placeholder(tf.float32)\n",
    "input2 = tf.placeholder(tf.float32)\n",
    "add = tf.add(input1, input2)\n",
    "\n",
    "with tf.Session() as session: # 利用Session执行上述代码中生成的DAG\n",
    "    res = session.run(add, feed_dict = {input1: [32.1], input2: 78.2}) # 同一个run中运行多个operation，operation之间可以有依赖关系\n",
    "    print(res)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
